namespace Admin2025.Domain.Entities.App;

// 应用角色实体
public class AppRole : EntityBase
{
    // 角色名称
    public string RoleName { get; set; } = null!;

    // 角色拥有的权限集合
    public ICollection<AppPermission> Permissions { get; set; } = new List<AppPermission>();

    // 拥有该角色的用户集合
    public ICollection<AppUser> Users { get; set; } = new List<AppUser>();

    /// <summary>
    /// 分配权限
    /// </summary>
    public void AssignPermission(AppPermission permission)
    {
        if (!Permissions.Contains(permission))
        {
            Permissions.Add(permission);
        }
    }

    /// <summary>
    /// 移除权限
    /// </summary>
    public void RemovePermission(AppPermission permission)
    {
        if (Permissions.Contains(permission))
        {
            Permissions.Remove(permission);
        }
    }

    /// <summary>
    /// 判断角色是否拥有指定权限编码的权限。
    /// </summary>
    /// <param name="permissionCode">权限编码。</param>
    /// <returns>如果角色拥有该权限，返回 true；否则返回 false。</returns>
    public bool HasPermission(string permissionCode)
    {
        return Permissions.Any(p => p.Code == permissionCode);
    }
}